package se.liu.johfa428.game.objects.death;

import java.awt.Color;

import se.liu.oschi129.animation.animationspace.AnimationSpace;
import se.liu.oschi129.font.fontspace.FontSpace;
import se.liu.oschi129.level.levelspace.LevelSpace;
import se.liu.oschi129.timer.Timer;
import se.liu.oschi129.world.model.World;
import se.liu.oschi129.world.model.WorldGraphics;
import se.liu.oschi129.world.objects.WorldStaticObject;

public class ObjectDeathScene extends WorldStaticObject{

	// Create three timers
	private Timer pre_timer, post_timer, gameover_timer;
	private World world;
	private boolean life_decreased = false, gameover = false;

	/**
	 * Creates the scene where the life is removed from the player when the character dies
	 * @param world
	 * Uses the World get the number of lives the character have.
	 */
	public ObjectDeathScene(World world) {
		super(Type.SOFT, AnimationSpace.get("img_game_over"), true, 0, 0, 320, 240);
		this.world = world;
		this.pre_timer = new Timer(30);
		this.post_timer = new Timer(60);
		this.gameover_timer = new Timer(100);
	}
	
	@Override public void performDrawingAction(WorldGraphics worldg) {
		if(gameover){
			// Draw game over image
			super.performDrawingAction(worldg);
		}else{
			// Draw text on screen
			worldg.setColor(Color.WHITE);
			worldg.setFont(FontSpace.get("font_status",10f));
			worldg.drawText(140, 120, "x ");
			worldg.drawText(150, 120, String.format("%02d",world.getLives()));
		}

	}

	@Override public void performStepAction(World world) {
		// Tick the timers
		pre_timer.tick();	
		post_timer.tick();
		if (gameover)
			gameover_timer.tick();
			
		// Check if the pre-timer is cool
		if (pre_timer.cool() && !life_decreased){
			// Decrease lives by one
			world.setLives(world.getLives() - 1);
			life_decreased = true;
		}
		
		// Check if the post-timer is cool
		if (post_timer.cool()){
			if(world.getLives() == 0){
				// The player has no more lives
				gameover = true;
			} else{
				// The player has more lives - return the the level selection
				world.clearSynchronized();
				world.popSynchronized();
			}
		}
		
		if(gameover_timer.cool()){
			// Restart game
			world.clearSynchronized();
			world.clearStack();		
			LevelSpace.get("level00_intro").createWorld(world, null);
		}
	}
}
